<?php
class IndexController extends Zendvn_Controller_Action {
	//Mang tham so nhan duoc o moi Action
	protected $_arrParam;
	
	//Duong dan cua Controller
	protected $_currentController;
	
	//Duong dan cua Action chinh
	protected $_actionMain;
	
	//Thong so phan trang
	protected $_paginator = array(
									'itemCountPerPage' => 20,
									'pageRange' => 4,
								  ); 
	protected $_namespace;
	
	public function init(){
		//Mang tham so nhan duoc o moi Action
		$this->_arrParam = $this->_request->getParams();
		
		//Duong dan cua Controller
		$this->_currentController = '/' . $this->_arrParam['module'] 
									 . '/' . $this->_arrParam['controller'];
		
		//Duong dan cua Action chinh		
		$this->_actionMain = '/' . $this->_arrParam['module'] 
							 . '/' . $this->_arrParam['controller']	. '/index';	

							 
		$this->_paginator['currentPage'] = $this->_request->getParam('page',1);
		$this->_arrParam['paginator'] = $this->_paginator;
		
		//Luu cac du lieu filter vao SESSION
		//Dat ten SESSION
		$this->_namespace = $this->_arrParam['module'] . '-' . $this->_arrParam['controller'];
		$ssFilter = new Zend_Session_Namespace($this->_namespace);
		//$ssFilter->unsetAll();
		if(empty($ssFilter->col)){
			$ssFilter->keywords 	= '';
		}
		$this->_arrParam['ssFilter']['keywords'] 	= $ssFilter->keywords;
		
		
		/*====================================================
		 * Load Zendvn Translate for Controller
		 *====================================================*/
		/*$this->_langObj->setLangFile(array('default.language.tmx'));		
		Zend_Registry::set('Zend_Translate', $this->_langObj->generate());*/
		
		$info = new Zendvn_System_Info();
		
		$language = $info->getLanguage('admin');
		if(empty($language)){
			$language = 'vi';
		}
		$translate = array(
							'adapter' => 'tmx',
							'content' => LANG_PATH . '/' . $language . '/admin/default.language.tmx',
							'locale' => $language,
							);
		$translate = new Zend_Translate($translate);
		Zend_Registry::set('Zend_Translate', $translate);
		
		
		
		//Truyen ra view
		$this->view->arrParam = $this->_arrParam;
		$this->view->currentController = $this->_currentController;
		$this->view->actionMain = $this->_actionMain;
		
		$template_path = TEMPLATE_PATH . "/public/system";
		$this->loadTemplate($template_path,'template.ini','template');
		
	}		
	public function indexAction() {	
	   $tblMenu				= new Zendvn_Models_Menus();
	   $menu				= $tblMenu->listItem($this->_arrParam,array('task'=>'index-home'));
	   $this->view->Title 	= $menu['name'];
	   if(empty($this->view->Title)) $this->view->Title = 'Nhà hàng Kim Hải';	   
	   $description 		= $menu['description_html'];
	   $keywords	 		= $menu['keywords_html'];
	   
	   $this->view->headTitle($this->view->Title,true);
	   if(!empty($description)) $this->view->headMeta(true)->setName('description',$description);	   
	   if(!empty($keywords)) $this->view->headMeta(true)->setName('keywords',$keywords);

	   $tblProduct 						= new Zendvn_Models_Products();
	   $this->view->Items   			= $tblProduct->listItem($this->_arrParam, Array('task'=>'public-list'));
	   $session 						= new Zend_Session_Namespace($this->_namespace);
	   $this->view->count_item_current 	= count($this->view->Items);
	   $session->count_item_current 	= $this->view->count_item_current;
	   $this->view->Count   			= $tblProduct->countItem($this->_arrParam, null);
	}
	public function aboutAction() {
		$tblAbout 						= new Zendvn_Models_About();
		$this->view->Item				= $tblAbout->listItem($this->_arrParam,array('task'=>'public-index'));
	   	$this->view->Title 				= $this->view->Item['title'];
	   	$this->view->headTitle($this->view->Title,true);
	}
	
	
	public function ajaxAddToCartAction(){
		$this->_helper->layout->disableLayout();
		$session = new Zend_Session_Namespace($this->_namespace);
		$sessionCart =  $session->shoppingcart;
		if(isset($this->_arrParam['id']) && $this->_arrParam['id'] >0){
			$add = 1;
			if(isset($this->_arrParam['count']) && $this->_arrParam['count'] >0) $add = $this->_arrParam['count']; 
			$strid = "id-".(string)$this->_arrParam['id'] ;
			if(array_key_exists($strid, (array)$sessionCart)){
				// if product id have areaddy in shopping cart
				$session->shoppingcart->$strid = $session->shoppingcart->$strid + $add;
			}else{
				$session->shoppingcart->$strid = $add;
			}
		}
	}
	public function ajaxLoadMoreAction(){
		$this->_helper->layout->disableLayout();
		$session = new Zend_Session_Namespace($this->_namespace);
		
		if(isset($this->_arrParam['limit']) && $this->_arrParam['limit'] >0){
			$tblProduct 					= new Zendvn_Models_Products();
	   		$this->view->Items   			= $tblProduct->listItem($this->_arrParam, Array('task'=>'public-list-loadmore'));
	   		$this->view->Count   			= $tblProduct->countItem($this->_arrParam, null);
	   		$session->count_item_current   += PRODUCT_TOLOADMORE;
	   		$this->view->count_item_current = $session->count_item_current;
		}else{
			$this->_helper->viewRenderer->setNoRender(true);
		}
	}
	public function ajaxRemoveFromCartAction(){
		$this->_helper->layout->disableLayout();
		$session = new Zend_Session_Namespace($this->_namespace);
		$sessionCart =  $session->shoppingcart;
		if(isset($this->_arrParam['id']) && $this->_arrParam['id'] >0){
			$strid = "id-".(string)$this->_arrParam['id'] ;
			if(array_key_exists($strid, (array)$sessionCart)){
				// if product id have areaddy in shopping cart, remove it
				unset($session->shoppingcart->$strid);
			}
		}
		if(isset($this->_arrParam['type']) && $this->_arrParam['type']=="redirect"){
			$urlOption = array("module"=>"products", "controller"=>"index", "action"=>"view-cart","lang"=>"vi");
			$url 	   = $this->view->url($urlOption,"viewcart",true);
			$this->_redirect("http://".$_SERVER['HTTP_HOST'].$url);
		}else
			$this->render("ajax-add-to-cart");
	}
	
	public function calendarAction() {	
		$tblCalendar = new Zendvn_Models_Calendar();
		$this->view->Items = $tblCalendar->listItem($this->_arrParam,array('task'=>'public-index')); 
	   	$this->view->Title = 'Bảng giá chung';
	   	$this->view->headTitle($this->view->Title,true);
	}
	public function calendarDetailAction() {	
		$tblCalendar = new Zendvn_Models_Calendar();
		$this->view->Item 	= $tblCalendar->listItem($this->_arrParam,array('task'=>'public-detail')); 
	   	$this->view->Title 	= $this->view->Item['title'];
	}
	public function viewAction() {
		$template_path = TEMPLATE_PATH . "/public/system";
		$this->loadTemplate($template_path,'template.ini','template');
	}	
	public function detailAction(){
		
	}
	public function contactAction(){
		$this->view->Title 		= 'Liên hệ';
		$this->view->headTitle($this->view->Title,true);
		$captcha 				= new Zendvn_Captcha_Image();
		//9.Truy gia tri cua CAPTCHA ra VIEW
		$this->view->captcha 	= $captcha->render($this->view);
		$this->view->captcha_id = $captcha->getId();
		
		if($this->_request->isPost()){
			$captchaID = $this->_arrParam['captchaID'];		
			$validator = new Default_Form_ValidateContact($this->_arrParam);
			if($validator->isError() == true){				
				$this->view->messageError = $validator->getMessageError();
				$this->view->Item = $validator->getData();		
			}
			else{
				$arrParam = $validator->getData();
				// Gui email
				$mail = new Zendvn_Mail();
				$success = $mail->sendContact($arrParam,null);
				if($success){
					$this->_helper->flashMessenger->addMessage('Chúng tôi đã nhận được liên hệ của bạn. Chúng tôi sẽ gửi email hồi đáp trong vòng 24h nữa. Chúc bạn một ngày tốt đẹp.');
					$this->_helper->_redirector('contact');
				}else{
					$this->_helper->flashMessenger->addMessage('Lỗi! Không thể gửi email được, vui lòng liên hệ BQT để được trợ giúp!');
					$this->_helper->_redirector('contact');
				}
			}
			$captcha->removeImg($captchaID);
		}
		$this->view->messages = $this->_helper->flashMessenger->getMessages();
		$captcha->removeImg($this->view->captcha_id,array('exception'=>true));
	}
	public function captchaImageAction(){
		$this->_helper->layout->disableLayout();
		$captcha = new Zendvn_Captcha_Image();
		$captchaID = $this->_request->getParam('captchaID',0);
		$captcha->removeImg($captchaID);
				
		$this->view->captcha = $captcha->render($this->view);
		$this->view->captcha_id = $captcha->getId();
	}
	
	public function questionsAction(){
		$this->view->Title = 'Gửi câu hỏi';
		$this->view->headTitle($this->view->Title,true);
		
		if($this->_request->isPost()){
			$validator = new Default_Form_ValidateQuestions($this->_arrParam);
			if($validator->isError() == true){
				$this->view->errors = $validator->getMessageError();
				$this->view->Item	= $validator->getData();
			}else{
				$tblQuestion = new Zendvn_Models_Questions();
				$arrParam = $validator->getData(array('upload'=>true));
				$tblQuestion -> saveItem($arrParam,array('task'=>'new'));
				$this->_redirect($this->_currentController . '/questions/status/success');
				
			}
		}
		//header("Location: http://dulichsacmau.com");
	}
	
	public function questionsAnswersAction(){
		$this->view->Title = 'Chia sẻ';
		$this->view->headTitle($this->view->Title,true);
		$tblQuestions = new Zendvn_Models_Questions();
		
		$this->view->Items = $tblQuestions->listItem($this->_arrParam, array('task'=>'index'));
		$totalItem  = $tblQuestions->countItem($this->_arrParam);		
		$paginator = new Zendvn_Paginator();
		$this->view->panigator = $paginator->createPaginator($totalItem,$this->_paginator);
	}
	
	public function answersDetailAction(){
		$tblQuestions  	= new Zendvn_Models_Questions();
		$tblAnswers	 	= new Zendvn_Models_Answers();
		$tblUser		= new Zendvn_Models_Users();
		$this->view->Answer = $tblAnswers->listItem($this->_arrParam, array('task'=>'answer'));
		$this->view->Item 	= $tblQuestions->getItem($this->_arrParam, array('task'=>'admin-edit'));
		$this->view->User 	= $tblQuestions->listItem($this->_arrParam, array('task'=>'detail'));
		
		$this->view->Title = $this->view->Item['title'];
		$this->view->headTitle($this->view->Title,true);
	}
}
